import itertools

from django.utils import simplejson
from google.appengine.ext.webapp import template

COLORS = (
    'blue',
    'red',
    )


def Serialize(obj):
  return obj


def AbsUriSplit(abs_uri):
  """
  """
  slash = '/'
  uri_parts = (part for part in abs_uri.split(slash) if part)

  try:
    first = uri_parts.next()
    href = '/%s/' % first
    yield first, href

    second = uri_parts.next()
    href += second
    yield second, href
  except StopIteration:
    return

  for part in uri_parts:
    href += slash + part
    yield part, href


def jsonify(object):
  # if isinstance(object, QuerySet):
  #   return Serialize(object)
  return simplejson.dumps(object)


def navify(path):
  return ' '.join([
      '<a class="navsegment %s" href=%s>%s</a>' % (clr, href, name)
      for clr, (name, href) in itertools.izip(
          itertools.cycle(COLORS), AbsUriSplit(path))])


def color_title(title):
  return '\n'.join([
      '<span class="%s">%s</span>' % (clr, char)
      for clr, char in itertools.izip(
          itertools.cycle(COLORS), title)])


register = template.create_template_register()
register.filter(jsonify)
register.filter(navify)
register.filter(color_title)
